package chapter03;


import java.awt.*;
import java.awt.event.KeyEvent;

import com.brackeen.javagamebook.graphics.*;
import com.brackeen.javagamebook.input.*;
import com.brackeen.javagamebook.test.GameCore;

/**
    InputManagerTest  tests the InputManager with a simple
    run-and-jump mechanism. The player moves and jumps using
    the arrow keys and the space bar.
    <p>Also, InputManagerTest demonstrates pausing a game
    by not updating the game elements if the game is paused.
*/
public class InputManagerTest extends GameCore {

    public static void main(String[] args) {
        new InputManagerTest().run();
    }

    protected GameAction jump;
    protected GameAction exit;
    protected GameAction moveLeft;
    protected GameAction moveRight;
    protected GameAction pause;
    protected GameAction alt;
    protected GameAction enter;


    protected InputManager inputManager;
    private Player player;
    private Image bgImage;
    private boolean paused;

    public void init() {
        System.out.println("init");
        super.init();
        Window window = screen.getFullScreenWindow();
        inputManager = new InputManager(window);

        // use these lines for relative mouse mode
//        inputManager.setRelativeMouseMode(true);
//        inputManager.setCursor(InputManager.INVISIBLE_CURSOR);

        createGameActions();
//        createSprite();
        paused = false;
        screen.setVisible(true);
        createSprite();

    }


    /**
        Tests whether the game is paused or not.
    */
    public boolean isPaused() {
        return paused;
    }


    /**
        Sets the paused state.
    */
    public void setPaused(boolean p) {
        if (paused != p) {
            this.paused = p;
//            inputManager.resetAllGameActions();
        }
    }


    public void update(long elapsedTime) {
        // check input that can happen whether paused or not
        checkSystemInput();

        if (!isPaused()) {
            // check game input
            checkGameInput();

            // update sprite
            player.update(elapsedTime);
        }
    }


    /**
        Checks input from GameActions that can be pressed
        regardless of whether the game is paused or not.
    */
    public void checkSystemInput() {
        if(alt.isPressed() && enter.isPressed()){
            if (screen.isFullScreen()) {
                System.out.println("退出全屏");
                screen.restoreScreen();
//                System.out.println("退出全屏后的 floorY"+player.getFloorY());
                System.out.println("退出全屏后的 screen.getHeight() "+screen.getHeight() );
                System.out.println("退出全屏后的 screen.getWidth() " + screen.getWidth());
            } else {
                DisplayMode displayMode =
                        screen.findFirstCompatibleMode(POSSIBLE_MODES);
                displayMode=ScreenManager.searchFullScreenModeDisplay(800 , 600);
                screen.setFullScreen(displayMode);
//                System.out.println("全屏后的 floorY"+player.getFloorY());
                System.out.println("找到适合的DM w:"+displayMode.getWidth()+
                        " h:"+displayMode.getHeight()+" r:"+displayMode.getRefreshRate()
                        +" b:"+displayMode.getBitDepth());
                System.out.println("全屏后的 screen.getHeight() " + screen.getHeight());
                System.out.println("全屏后的 screen.getWidth() " + screen.getWidth());

            }
        }
        if (pause.isPressed()) {
            setPaused(!isPaused());
        }
        if (exit.isPressed()) {
            stop();
                

        }
    }


    /**
        Checks input from GameActions that can be pressed
        only when the game is not paused.
    */
    public void checkGameInput() {
        float velocityX = 0;
        if (moveLeft.isPressed()) {
            velocityX-=Player.SPEED;
        }
        if (moveRight.isPressed()) {
            velocityX+=Player.SPEED;
        }
        player.setVelocityX(velocityX);

        if (jump.isPressed() &&
            player.getState() != Player.STATE_JUMPING)
        {
            player.jump();
        }
    }


    public void draw(Graphics2D g) {
        // draw background
        g.drawImage(bgImage, 0, 0, null);
//        System.out.println("x " + player.getX());
//        System.out.println("y " + player.getY());

        // draw sprite
        g.drawImage(player.getImage(),
            Math.round(player.getX()),
            Math.round(player.getY()),
            null);
    }

//GameAction mouse_move_left;
//GameAction mouse_move_right;
    /**
        Creates GameActions and maps them to keys.
    */
    public void createGameActions() {
//        mouse_move_left=new GameAction("mouse_l");
//        mouse_move_right=new GameAction("mouse_r");
//        inputManager.mapToMouse(mouse_move_left, InputManager.MOUSE_MOVE_LEFT);
//        inputManager.mapToMouse(mouse_move_right, InputManager.MOUSE_MOVE_RIGHT);
        
        
        jump = new GameAction("jump"); /*,
            GameAction.DETECT_INITAL_PRESS_ONLY*/
        exit = new GameAction("exit",
            GameAction.DETECT_INITAL_PRESS_ONLY);
        moveLeft = new GameAction("moveLeft");
        moveRight = new GameAction("moveRight");
        pause = new GameAction("pause",
            GameAction.DETECT_INITAL_PRESS_ONLY);
        alt=new GameAction("alt");
        enter=new GameAction("enter",GameAction.DETECT_INITAL_PRESS_ONLY);
        
        inputManager.mapToKey(alt, KeyEvent.VK_ALT);
        inputManager.mapToKey(enter, KeyEvent.VK_ENTER);

        inputManager.mapToKey(exit, KeyEvent.VK_ESCAPE);
        inputManager.mapToKey(pause, KeyEvent.VK_P);

        // jump with spacebar or mouse button
        inputManager.mapToKey(jump, KeyEvent.VK_SPACE);
        inputManager.mapToMouse(jump,
            InputManager.MOUSE_BUTTON_1);

        // move with the arrow keys...
        inputManager.mapToKey(moveLeft, KeyEvent.VK_LEFT);
        inputManager.mapToKey(moveRight, KeyEvent.VK_RIGHT);

        // ... or with A and D.
        inputManager.mapToKey(moveLeft, KeyEvent.VK_A);
        inputManager.mapToKey(moveRight, KeyEvent.VK_D);

//        // use these lines to map player movement to the mouse
//        inputManager.mapToMouse(moveLeft,
//          InputManager.MOUSE_MOVE_LEFT);
//        inputManager.mapToMouse(moveRight,
//          InputManager.MOUSE_MOVE_RIGHT);

    }


    /**
        Load images and creates the Player sprite.
    */
    private void createSprite() {
        // load images
        bgImage = loadImage("images/background.jpg");
        Image player1 = loadImage("images/player1.png");
        Image player2 = loadImage("images/player2.png");
        Image player3 = loadImage("images/player3.png");

        // create animation
        Animation anim = new Animation();
        anim.addFrame(player1, 250);
        anim.addFrame(player2, 150);
        anim.addFrame(player1, 150);
        anim.addFrame(player2, 150);
        anim.addFrame(player3, 200);
        anim.addFrame(player2, 150);

        player = new Player(anim);
        
        player.setFloorY(/*screen.getHeight()in non-fullscreen this size isn't exact */
                screen.getFullScreenWindow().getHeight()
                - player.getHeight());
        System.out.println("screen.getFullScreenWindow().getContentPane()"
                +  screen.getFullScreenWindow().getContentPane().getHeight());
//        System.out.println("f "+player.getFloorY());
    }

}
